package com.ptswitch.material.model.security;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.validation.constraints.Size;

import org.hibernate.annotations.Cascade;
import org.hxzon.project.BaseEntity;

import com.ptswitch.material.util.security.RoleEditUtil;
import com.ptswitch.material.util.security.RoleUtil;

@Entity
public class Role extends BaseEntity {

    private static final long serialVersionUID = -878783539334292324L;

//    @ManyToMany(fetch = FetchType.EAGER)
//    @JoinTable(name = "role_permission", //
//    joinColumns = @JoinColumn(name = "role_id"), //
//    inverseJoinColumns = @JoinColumn(name = "permission_id"))
//    @OrderBy("permissionName")
//    private List<Permission> permissions;

    @OneToMany(mappedBy = "role", fetch = FetchType.EAGER)
    @Cascade(value = { org.hibernate.annotations.CascadeType.DELETE, //
            org.hibernate.annotations.CascadeType.PERSIST })
    private List<RolePermission> rolePermissions;

    @OneToMany(mappedBy = "role", fetch = FetchType.LAZY)
    private List<UserRole> userRoles;

    @Column(columnDefinition = DefineVarchar32)
    @Size(max = DefineSize32)
    private String roleName;

    @Column(columnDefinition = DefineVarchar32)
    @Size(max = DefineSize32)
    private String displayName;

    @Column(columnDefinition = DefineVarchar16)
    @Size(max = DefineSize16)
    private String roleGroup = RoleUtil.RoleGroupName_Custom;

    private boolean inUse = true;

    @Override
    public String toString() {
        return displayName + "[" + roleName + "]";
    }

    public void addPermission(Permission permission) {
        this.rolePermissions.add(new RolePermission(this, permission));
    }

    public List<Permission> getPermissions() {
        return RoleEditUtil.getPermissions(this);
    }

    //=====================
    public Role() {
    }

    public Role(String roleName, String displayName, String roleGroup) {
        this.roleName = roleName;
        this.displayName = displayName;
        this.roleGroup = roleGroup;
    }

    //
    public List<RolePermission> getRolePermissions() {
        return rolePermissions;
    }

    public void setRolePermissions(List<RolePermission> rps) {
        this.rolePermissions = rps;
    }

    public List<UserRole> getUserRoles() {
        return userRoles;
    }

    public void setUserRoles(List<UserRole> userRoles) {
        this.userRoles = userRoles;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getRoleGroup() {
        return roleGroup;
    }

    public void setRoleGroup(String roleGroup) {
        this.roleGroup = roleGroup;
    }

    public boolean isInUse() {
        return inUse;
    }

    public void setInUse(boolean inUse) {
        this.inUse = inUse;
    }

}
